
import common.Mask;
import common.commonFunction;

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.FileReference;
import flash.net.URLRequest;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import prestagingDataUpload.SuccessAlert;


public var downloadFileRef:FileReference;

[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
[Embed(source="images/reset.png")]private var resetIcon:Class;
[Embed(source="images/question.png")]private var questionIcon:Class;

[Bindable]public var fileName:String="";
[Bindable]public var urlStream:URLStream;
[Bindable]public var urlPrefix:String;
[Bindable]public var filePath:String;
[Bindable]public var filePath2:String;
[Bindable]public var params:Object={};
[Bindable]public var entityXml:XML;
[Bindable]public var programXml:XML;
[Bindable]public var branchXml:XML;
[Bindable]public var specializationXml:XML;
[Bindable]public var semesterXml:XML;

public function OnCreatPage():void
{
	urlPrefix=commonFunction.getConstants('url')+"/prestagingData/";
	filePath=commonFunction.getConstants('url')+"/PrestagingDataDocuments/";
	filePath2=commonFunction.getConstants('url')+"/UploadedPrestagingDataDocuments/";
	
	params['time']=new Date;
	params['mode']="all";
	getEntityList.send(params);
}

public function getEntitySuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	
	entityXml=event.result as XML;
	entityCombo.dataProvider=entityXml.Details.name;
	entityCombo.selectedIndex=-1;
}

public function onEntityChange(event:Event):void
{
	params['time']=new Date;
	params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
	getProgramList.send(params);
	branchCombo.selectedIndex=-1;
	specilizationCombo.selectedIndex=-1;
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	branchCombo.enabled=false;
	specilizationCombo.enabled=false;
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
    downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function getProgramSuccess(event:ResultEvent):void
{
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	programXml=event.result as XML;
	
	programCombo.dataProvider=programXml.Details.name;
	programCombo.selectedIndex=-1;
	programCombo.enabled=true;
}

public function onProgramChange(event:Event):void
{
	params['time']=new Date;
	params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
	params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
	getBranchList.send(params);
	specilizationCombo.selectedIndex=-1;
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	specilizationCombo.enabled=false;
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
    downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function getBranchSuccess(event:ResultEvent):void
{
	
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	branchXml=event.result as XML;
	branchCombo.dataProvider=branchXml.Details.name;
	branchCombo.selectedIndex=-1;
	branchCombo.enabled=true;
}

public function onBranchChange(event:Event):void
{
	params['time']=new Date;
	params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
	params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
	params['branchId']=branchXml.Details.(name==branchCombo.selectedLabel).id;
	getSpecializationList.send(params);
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
    downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function getSpecializationSuccess(event:ResultEvent):void
{
	
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	specializationXml=event.result as XML;
	specilizationCombo.dataProvider=specializationXml.Details.name;
	specilizationCombo.selectedIndex=-1;
	specilizationCombo.enabled=true;
}

public function onSpecializationChange(event:Event):void
{
	params['time']=new Date;
	params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
	params['branchId']=branchXml.Details.(name==branchCombo.selectedLabel).id;
	params['specializationId']=specializationXml.Details.(name==specilizationCombo.selectedLabel).id;
	getSemesterList.send(params);
	startDateField.text="";
	endDateField.text="";
	startDateField.enabled=false;
	endDateField.enabled=false;
    downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function getSemesterSuccess(event:ResultEvent):void
{
	
	if(event.result.sessionConfirm == true)
	{
		Alert.show(resourceManager.getString('Messages','sessionInactive'),commonFunction.getMessages('error'),0,null,null,errorIcon);
	   	this.parentDocument.vStack.selectedIndex=0;
	   	this.parentDocument.loaderCanvas.removeAllChildren();
	}
	semesterXml=event.result as XML;
	semesterCombo.dataProvider=semesterXml.role.description;
	semesterCombo.selectedIndex=-1;
	semesterCombo.enabled=true;
}

public function onFailure(event:FaultEvent):void
{
	Alert.show(event.message+commonFunction.getMessages('error'),commonFunction.getMessages('error'),0,null,null,errorIcon);  	
}

public function onSemesterChange(event:Event):void
{
    downloadButton.enabled=false;
    submitButton.enabled=false;
	startDateField.text="";
	endDateField.text="";
	startDateField.enabled=true;
	endDateField.enabled=false;
}

public function onStartDateChange():void
{
	endDateField.text="";
	endDateField.enabled=true;
	downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function onEndDateChange():void
{
	downloadButton.enabled=false;
    submitButton.enabled=false;
	
	fileName=entityXml.Details.(name==entityCombo.selectedLabel).id;
	fileName+="_"+programXml.Details.(name==programCombo.selectedLabel).id;
	fileName+="_"+branchXml.Details.(name==branchCombo.selectedLabel).id;
	fileName+="_"+specializationXml.Details.(name==specilizationCombo.selectedLabel).id;
	fileName+="_"+semesterXml.role.(description==semesterCombo.selectedLabel).id;
	fileName+="_"+startDateField.text;
	fileName+="_"+endDateField.text+"_prestagingData.xls";
	
	urlStream=new URLStream();
	urlStream.addEventListener(Event.COMPLETE, streamHandler);
	urlStream.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
	urlStream.load(new URLRequest(filePath+fileName));
}

public function downloadFile(event:Event):void
{
	downloadFileRef=new FileReference;
    downloadFileRef.download(new URLRequest(filePath+fileName));
}

public function streamHandler(e:Event):void
{
    urlStream.close();
    downloadButton.enabled=true;
    submitButton.enabled=true;
}

public function onFound(e:Event):void
{
    urlStream.close();
    Alert.show("File And Data Already Uploaded For This Combination");
    downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function onNotFound(event:IOErrorEvent):void
{
    urlStream.close();
    Alert.show("File Not Uploaded For This Combination Till Now");
    downloadButton.enabled=false;
    submitButton.enabled=false;
}

public function onIOError(event:IOErrorEvent):void
{
	urlStream=new URLStream();
	urlStream.addEventListener(Event.COMPLETE, onFound);
	urlStream.addEventListener(IOErrorEvent.IO_ERROR, onNotFound);
	urlStream.load(new URLRequest(filePath2+fileName));
}

public function submitData(event:Event):void
{	
	Alert.show(commonFunction.getMessages('conformForContinue'),commonFunction.getMessages('confirm'),(Alert.YES|Alert.NO),null,insertOrNot,questionIcon);
}

public function insertOrNot(event:CloseEvent):void
{
	if(event.detail==Alert.YES)
	{
		params['fileName']=fileName;
		params['entityId']=entityXml.Details.(name==entityCombo.selectedLabel).id;
		params['programId']=programXml.Details.(name==programCombo.selectedLabel).id;
		params['branchId']=branchXml.Details.(name==branchCombo.selectedLabel).id;
		params['specializationId']=specializationXml.Details.(name==specilizationCombo.selectedLabel).id;
		params['semesterId']=semesterXml.role.(description==semesterCombo.selectedLabel).id;
		params['semesterStartDate']=startDateField.text;
		params['semesterEndDate']=endDateField.text;
		params['time']=new Date;
		setPrestagingData.send(params);
		Mask.show(commonFunction.getMessages('pleaseWait'));
	}
}

public function setPrestagingDataSuccess(event:ResultEvent):void
{
	Mask.close();
	var successList:SuccessAlert=SuccessAlert(PopUpManager.createPopUp(this,SuccessAlert,false));
	successList.dataArray=event.result.student;
	PopUpManager.centerPopUp(successList);
	
	entityCombo.selectedIndex=-1;
	programCombo.selectedIndex=-1;
	branchCombo.selectedIndex=-1;
	specilizationCombo.selectedIndex=-1;
	semesterCombo.selectedIndex=-1;
	startDateField.text="";
	endDateField.text="";
	programCombo.enabled=false;
	branchCombo.enabled=false;
	specilizationCombo.enabled=false;
	semesterCombo.enabled=false;
	startDateField.enabled=false;
	endDateField.enabled=false;
    downloadButton.enabled=false;
    submitButton.enabled=false;
}